home *** CD-ROM | disk | FTP | other *** search
/ 3D Games (Spidla) / 3dhry2.iso / Tank Wars 3D / src / INIT.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  2003-03-17  |  8.7 KB  |  343 lines

  1. #include <time.h>
  2. #include <math.h>
  3. #include <fstream.h>
  4. #include <dinput.h>
  5.  
  6. #include "tankwars.h"
  7.  
  8. GLvoid BuildLists(void);
  9. int DI_Init();
  10.  
  11. extern GLuint texture[TOTAL_TEXTURES];
  12.  
  13. extern int heightmap[31][21];
  14. extern unsigned char lightmap[30][20];
  15. extern int grid[30][20];
  16.  
  17. float explosionmap[20][20];
  18.  
  19. extern float cameraangle;
  20.  
  21. extern c_player player[2];
  22.  
  23. extern c_explosion explosion[50];
  24. extern int nextexplosionslot;
  25.  
  26. extern c_missle missle[20];
  27. extern int nextmissleslot;
  28.  
  29. extern c_smoke smoke[50];
  30. extern int nextsmokeslot;
  31.  
  32. extern c_grenade grenade[20];
  33. extern int nextgrenadeslot;
  34.  
  35. extern c_particle particle[500];
  36. extern int nextparticleslot;
  37.  
  38. extern c_debris debris[100];
  39. extern int nextdebrisslot;
  40.  
  41. extern unsigned long wall;
  42.  
  43. extern c_rain rain[500];
  44.  
  45. extern int poweruptime;
  46.  
  47. extern int winner;
  48.  
  49. extern int fogenabled;
  50. extern int timeofday;
  51. extern int raindensity;
  52.  
  53. void gameinit(void)
  54. {
  55.     int i, x, y;
  56.  
  57.     srand(time(0));
  58.     
  59.     player[0].control.forward=DIK_UP;
  60.     player[0].control.backward=DIK_DOWN;
  61.     player[0].control.left=DIK_LEFT;
  62.     player[0].control.right=DIK_RIGHT;
  63.     player[0].control.leftchange=DIK_PERIOD;
  64.     player[0].control.rightchange=DIK_SLASH;
  65.     player[0].control.fire=DIK_RSHIFT;
  66.  
  67.     player[1].control.forward=DIK_R;
  68.     player[1].control.backward=DIK_F;
  69.     player[1].control.left=DIK_D;
  70.     player[1].control.right=DIK_G;
  71.     player[1].control.leftchange=DIK_LSHIFT;
  72.     player[1].control.rightchange=DIK_Z;
  73.     player[1].control.fire=DIK_X;
  74.  
  75.     player[0].control.rightf=0;
  76.     player[0].control.leftf=0;
  77.     player[1].control.rightf=0;
  78.     player[1].control.leftf=0;
  79.  
  80.     player[0].mode=BRICK_MODE;
  81.     player[1].mode=BRICK_MODE;
  82.  
  83.     player[0].startpos.x=5;
  84.     player[0].startpos.y=10;
  85.     player[1].startpos.x=25;
  86.     player[1].startpos.y=10;
  87.  
  88.     player[0].pos.x=(float)(player[0].startpos.x*10+5);
  89.     player[0].pos.y=(float)(player[0].startpos.y*10+5);
  90.     player[0].rot=0;
  91.     
  92.     player[1].pos.x=(float)(player[1].startpos.x*10+5);
  93.     player[1].pos.y=(float)(player[1].startpos.y*10+5);
  94.     player[1].rot=180;
  95.  
  96.     player[0].lives=5;
  97.     player[1].lives=5;
  98.  
  99.     player[0].ammo.bricks=15;
  100.     player[0].ammo.mines=15;
  101.     player[0].ammo.missles=10;
  102.     player[0].ammo.grenades=5;
  103.     player[1].ammo.bricks=10;
  104.     player[1].ammo.mines=10;
  105.     player[1].ammo.missles=10;
  106.     player[1].ammo.grenades=5;
  107.  
  108.     player[0].missleload=0;
  109.     player[1].missleload=0;
  110.  
  111.     winner=2;
  112.  
  113.     for(y=0; y<21; y++)
  114.     {
  115.         for(x=0; x<31; x++)
  116.         {
  117.             heightmap[x][y]=rand()%6;
  118.             grid[x][y]=0;
  119.         }
  120.     }
  121.  
  122.     for(i=0; i<50; i++)
  123.     {
  124.         x=rand()%30;
  125.         y=rand()%20;
  126.         if(grid[x][y]==EMPTY)
  127.             grid[x][y]=MINE;
  128.     }
  129.     for(i=0; i<10; i++)
  130.     {
  131.         x=rand()%30;
  132.         y=rand()%20;
  133.         if(grid[x][y]==EMPTY)
  134.             grid[x][y]=BRICK1;
  135.     }
  136.     for(i=0; i<10; i++)
  137.     {
  138.         x=rand()%30;
  139.         y=rand()%20;
  140.         if(grid[x][y]==EMPTY)
  141.             grid[x][y]=BRICK0;
  142.     }
  143.     for(i=0; i<50; i++)
  144.     {
  145.         x=rand()%30;
  146.         y=rand()%20;
  147.         if(grid[x][y]==EMPTY)
  148.             grid[x][y]=TREE;
  149.     }
  150.     for(i=0; i<50; i++)
  151.     {
  152.         x=rand()%30;
  153.         y=rand()%20;
  154.         if(grid[x][y]==EMPTY)
  155.             grid[x][y]=TRUNK;
  156.     }
  157.  
  158.     grid[player[0].startpos.x][player[0].startpos.y]=REDFLAG;
  159.     grid[player[1].startpos.x][player[1].startpos.y]=BLUEFLAG;
  160.  
  161.     grid[player[0].startpos.x+3][player[0].startpos.y]=REDGATE;
  162.     grid[player[1].startpos.x-3][player[1].startpos.y]=BLUEGATE;
  163.  
  164.     nextexplosionslot=0;
  165.     for(i=0; i<50; i++)
  166.         explosion[i].stage=0;
  167.  
  168.     nextmissleslot=0;
  169.     for(i=0; i<20; i++)
  170.         missle[i].active=0;
  171.  
  172.     nextsmokeslot=0;
  173.     for(i=0; i<50; i++)
  174.         smoke[i].active=0;
  175.  
  176.     nextgrenadeslot=0;
  177.     for(i=0; i<20; i++)
  178.         grenade[i].stage=0;
  179.  
  180.     nextparticleslot=0;
  181.     for(i=0; i<500; i++)
  182.         particle[i].life=0;
  183.  
  184.     nextdebrisslot=0;
  185.     for(i=0; i<100; i++)
  186.         debris[i].active=0;
  187.  
  188.     for(i=0; i<500; i++)
  189.     {
  190.         rain[i].pos.x=(float)(rand()%300);
  191.         rain[i].pos.y=(float)(rand()%200);
  192.         rain[i].pos.z=(float)(rand()%100);
  193.     }
  194.  
  195.     poweruptime=125;
  196.  
  197.     wall=300;
  198.  
  199.     cameraangle=20;
  200.  
  201.     fogenabled=0;
  202.     timeofday=1;
  203.     raindensity=0;
  204.  
  205.     ifstream fin("data/explosionmap.twl");
  206.     for(y=0; y<20; y++)
  207.     {
  208.         for(x=0; x<20; x++)
  209.         {
  210.             fin >> explosionmap[x][y];
  211.         }
  212.     }
  213.  
  214.     calclightmap();
  215. }
  216.  
  217. // All Setup For OpenGL Goes Here:
  218. int InitGL(GLvoid)
  219. {
  220.     if (!DI_Init())                                        // Initialize DirectInput
  221.         return 0;
  222.     if (!LoadGLTextures())                                // Jump To Texture Loading Routine
  223.         return FALSE;                                    // If Texture Didn't Load Return FALSE
  224.     BuildFont();                                        // Build The Font
  225.     BuildLists();                                        // Jump To The Code That Creates Our Display Lists
  226.     glBlendFunc(GL_SRC_ALPHA,GL_ONE);                    // Select The Type Of Blending
  227.     glEnable(GL_TEXTURE_2D);                            // Enable Texture Mapping
  228.     glShadeModel(GL_SMOOTH);                            // Enable Smooth Shading
  229.     glCullFace(GL_BACK);
  230.     glDisable(GL_CULL_FACE);
  231.     glClearColor(0.0f, 0.0f, 0.0f, 0.5f);                // Black Background
  232.     glClearDepth(1.0f);                                    // Depth Buffer Setup
  233.     glEnable(GL_DEPTH_TEST);                            // Enables Depth Testing
  234.     glDepthFunc(GL_LEQUAL);                                // The Type Of Depth Testing To Do
  235.     glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);    // Really Nice Perspective Calculations
  236.  
  237.     return TRUE;                                        // Initialization Went OK
  238. }
  239.  
  240.  
  241. // Load Bitmaps And Convert To Textures:
  242. int LoadGLTextures(void)
  243. {
  244.     int Status=FALSE, i;                                    // Status Indicator
  245.     AUX_RGBImageRec *TextureImage[TOTAL_TEXTURES];                    // Create Storage Space For The Textures
  246.     memset(TextureImage,0,sizeof(void *)*TOTAL_TEXTURES);            // Set The Pointer To NULL
  247.     if ((TextureImage[FONT_TEXTURE]=LoadBMP("Data/Font.bmp")) &&
  248.         (TextureImage[GROUND_TEXTURE]=LoadBMP("Data/Ground.bmp")) &&
  249.         (TextureImage[MINE_TEXTURE]=LoadBMP("Data/Mine.bmp")) &&
  250.         (TextureImage[BRICK1_TEXTURE]=LoadBMP("Data/Brick1.bmp")) &&
  251.         (TextureImage[BRICK0_TEXTURE]=LoadBMP("Data/Brick0.bmp")) &&
  252.         (TextureImage[TRUNK_TEXTURE]=LoadBMP("Data/Trunk.bmp")) &&
  253.         (TextureImage[TREE_TEXTURE]=LoadBMP("Data/Tree.bmp")) &&
  254.         (TextureImage[REDTANK_TEXTURE]=LoadBMP("Data/Tankred.bmp")) &&
  255.         (TextureImage[BLUETANK_TEXTURE]=LoadBMP("Data/Tankblue.bmp")) &&
  256.         (TextureImage[DEBRIS_TEXTURE]=LoadBMP("Data/Debris.bmp")) &&
  257.         (TextureImage[WALL_TEXTURE]=LoadBMP("Data/Wall.bmp")) &&
  258.         (TextureImage[MISSLE_TEXTURE]=LoadBMP("Data/Missle.bmp")) &&
  259.         (TextureImage[SMOKE_TEXTURE]=LoadBMP("Data/Smoke.bmp")) &&
  260.         (TextureImage[LIFE_POWERUP_TEXTURE]=LoadBMP("Data/Lifepu.bmp")) &&
  261.         (TextureImage[BRICK_POWERUP_TEXTURE]=LoadBMP("Data/Brickpu.bmp")) &&
  262.         (TextureImage[MINE_POWERUP_TEXTURE]=LoadBMP("Data/Minepu.bmp")) &&
  263.         (TextureImage[MISSLE_POWERUP_TEXTURE]=LoadBMP("Data/Misslepu.bmp")) &&
  264.         (TextureImage[GRENADE_POWERUP_TEXTURE]=LoadBMP("Data/Grenadpu.bmp")))
  265.     {   
  266.         Status=TRUE;                                    // Set The Status To TRUE
  267.         glGenTextures(TOTAL_TEXTURES, &texture[0]);                    // Create The Texture
  268.         for (i=0; i<TOTAL_TEXTURES; i++)
  269.         {
  270.             glBindTexture(GL_TEXTURE_2D, texture[i]);
  271.             glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[i]->sizeX, TextureImage[i]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[i]->data);
  272.             glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
  273.             glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
  274.         }
  275.         for (i=0; i<TOTAL_TEXTURES; i++)
  276.         {
  277.             if (TextureImage[i])                    // If Texture Exists
  278.             {
  279.                 if (TextureImage[i]->data)            // If Texture Image Exists
  280.                 {
  281.                     free(TextureImage[i]->data);    // Free The Texture Image Memory
  282.                 }
  283.                 free(TextureImage[i]);                // Free The Image Structure 
  284.             }
  285.         }
  286.     }
  287.     return Status;                                        // Return The Status
  288. }
  289.  
  290. void calclightmap(void)
  291. {
  292.     float Ax, Ay, Az, Bx, By, Bz, normalx, normaly, normalz, l;
  293.     int x, y, L1, L2;
  294.  
  295.     for(y=0; y<20; y++)
  296.     {
  297.         for(x=0; x<30; x++)
  298.         {
  299.             Ax=(float)((x*10-150)-(x*10-140));
  300.             Ay=(float)((y*10-100)-(y*10-100));
  301.             Az=(float)(heightmap[x][y]-heightmap[x+1][y]);
  302.             Bx=(float)((x*10-150)-(x*10-140));
  303.             By=(float)((y*10-90)-(y*10-100));
  304.             Bz=(float)(heightmap[x][y+1]-heightmap[x+1][y]);
  305.  
  306.             normalx=(Ay*Bz)-(By*Az);
  307.             normaly=(Az*Bx)-(Bz*Ax);
  308.             normalz=(Ax*By)-(Bx*Ay);
  309.  
  310.             l=(float)sqrt(normalx*normalx + normaly*normaly + normalz*normalz);
  311.             normalx=normalx/l;
  312.             normaly=normaly/l;
  313.             normalz=normalz/l;
  314.  
  315.             L1=(int)((float)normalz*(float)255);
  316.  
  317.             Ax=(float)((x*10-140)-(x*10-140));
  318.             Ay=(float)((y*10-100)-(y*10-90));
  319.             Az=(float)(heightmap[x+1][y]-heightmap[x+1][y+1]);
  320.             Bx=(float)((x*10-150)-(x*10-140));
  321.             By=(float)((y*10-90)-(y*10-90));
  322.             Bz=(float)(heightmap[x][y+1]-heightmap[x+1][y+1]);
  323.  
  324.             normalx=(Ay*Bz)-(By*Az);
  325.             normaly=(Az*Bx)-(Bz*Ax);
  326.             normalz=(Ax*By)-(Bx*Ay);
  327.  
  328.             l=(float)sqrt(normalx*normalx + normaly*normaly + normalz*normalz);
  329.             normalx=normalx/l;
  330.             normaly=normaly/l;
  331.             normalz=normalz/l;
  332.  
  333.             L2=(int)((float)normalz*(float)255);
  334.  
  335.             lightmap[x][y]=(L1+L2)/2;
  336.  
  337.             if(lightmap[x][y]<70)
  338.                 lightmap[x][y]+=180;
  339.             lightmap[x][y]=(unsigned char)(lightmap[x][y]*.4);
  340.         }
  341.     }
  342. }
  343.